package chpt10;
import javax.swing.*;
import java.awt.*;

public class JLabelIconExample extends JFrame {
    public JLabelIconExample() {
        setTitle("JLabel Icon Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        // 创建 ImageIcon 对象，加载图片文件
        ImageIcon icon = new ImageIcon("images/jmu.jpg");
        

        // 创建 JLabel 对象，并设置图标
        JLabel label = new JLabel();
        label.setIcon(icon);
        label.setPreferredSize(new Dimension(300, 200));
        label.setIconTextGap(5);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        // 添加 JLabel 到窗口
        add(label);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JLabelIconExample();
            }
        });
    }
}